/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller.CoreServlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Model.BeanUltility.*;
import Model.ObjectBean.RequestBean.*;
import Model.ObjectBean.ViewBean.*;
import java.sql.ResultSet;
import Model.DataUltility.*;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.Session;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Tran Nguyen Anh Tu
 * Function : verify user account informations 
 * How to work :
 * 1. Receive Account Informations from client and populate to AccountInfo_FrClient
 * 2. Look for in database!
 * 3. Compare and return result for User
 */
public class LoginServlet extends HttpServlet {

    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
               // response.setHeader("Cache-Control", "no-cache");
                //response.setHeader("Cache-Control", "no-store");        
                //response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
                //response.setDateHeader("Expires", 0); // Proxies.
            UserAccount_FrClient clientInfo= new UserAccount_FrClient();
            //LoginStatus status =new LoginStatus();
            LoginStatus status = (LoginStatus) request.getSession().getAttribute("status");
            //PersonalInfo_FrDatabase perInfo = new PersonalInfo_FrDatabase();
            PersonalInfo_FrDatabase perInfo=(PersonalInfo_FrDatabase) request.getSession().getAttribute("perInfo");
            Data_Access accessDB = new Data_Access();
            /*
            LoginStatus status =new LoginStatus();
            PersonalInfo_FrDatabase perInfo= new PersonalInfo_FrDatabase();
           
            
            request.getSession().setAttribute("status", status);
            request.getSession().setAttribute("perInfo", perInfo);*/
            
            RequestDispatcher dispatcher =request.getRequestDispatcher("/WEB-INF/MainJSP/LoginPage.jsp");

        try {
           
       
            //Step1 
            PopulateBean.populateBeans(clientInfo,request);
            
            //Step2
            String []column={"id","passw"};
            String []data={clientInfo.getUsername(),clientInfo.getPassword()};
            ResultSet rs = accessDB.getInformation("login_account", column, data);
            
            //Step2
            String []column1={"id"};
            String []data1={clientInfo.getUsername()};
            ResultSet rs1 =accessDB.getInformation("personal_info",column1,data1);
            ResultsetMapper.mapperResultsettoObject(rs1,PersonalInfo_FrDatabase.class,perInfo);
            
            //Step 3
            rs.last();
            int count=rs.getRow();             
            if(count==1)
            {              
                status.setStatus("true");               
                request.getSession().setAttribute("perInfo",perInfo);                          
                dispatcher.forward(request, response);
            }
            else
            {  
                status.setStatus("false");
                dispatcher.forward(request, response);
            }
        } catch (Exception ex) {
            Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
            dispatcher.forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req, resp);
    }

    
}
